﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Bill;
using OpenSoftStore.BusinessLogic.Member;
using OpenSoftStore.Client.Common;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Orm.View;

namespace OpenSoftStore.Client.UIL.ManageMember.Info
{
    public partial class Dinner : UserControl
    {
        private static Dinner instance;
        public DinnerSaleLogic DinnerSaleBll;
        public T_Member Member;
        public List<V_DinnerSale> DinnerSales;
        public BillLogic LgcBill; 

        public static Dinner Instance
        {
            get { return instance ?? (instance = new Dinner()); }
        }

        public Dinner()
        {
            InitializeComponent();
            SPACommon.SetUiStyle(this);
            DinnerSaleBll=new DinnerSaleLogic();
            LgcBill = new BillLogic();
            Member=new T_Member();
            DinnerSales = new List<V_DinnerSale>();
        }

        public void GetDinner(T_Member member)
        {
            Member = member;
            dgvDinnerSale.AutoGenerateColumns = false;
            DinnerSales = DinnerSaleBll.GetDinnerSalesView(" where t1.MemberId ='" + member.Id + "'");
            dgvDinnerSale.DataSource = DinnerSaleBll.GetDinnerSalesDataTable(" where t1.MemberId ='" + member.Id + "'");
        }

        private void dgvDinnerSale_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvDinnerSale.CurrentRow == null) return;
            var id = dgvDinnerSale.CurrentRow.Cells["DinnerSaleId"].Value.ToString();
            var data= DinnerSales.FirstOrDefault(m=>m.Id == id) ?? new V_DinnerSale();
            SPACommon.SetObjectToControlValue(this, data);

            var dinnerSaleId = "";
            if (data != null) dinnerSaleId = data.Id;
            dgvDinnerSaleItem.AutoGenerateColumns = false;
            dgvDinnerSaleItem.DataSource = DinnerSaleBll.GetDinnerSaleItemsView(" where T_DinnerSaleItem.DinnerSaleId = '" + dinnerSaleId + "'");
            dgvDinnerSaleItem.AutoGenerateColumns = false;
            dgvDinnerSaleUse.DataSource = LgcBill.GetBillDinnerSaleView(" where T_BillItem.DinnerSaleId = '" + dinnerSaleId + "'");
        
        }

        private void dgvDinnerSale_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dgvDinnerSaleUse_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvDinnerSale.CurrentRow == null) return;
            var id = dgvDinnerSale.CurrentRow.Cells["DinnerSaleId"].Value.ToString();
            var data = DinnerSales.FirstOrDefault(m => m.Id == id) ?? new V_DinnerSale();
            SPACommon.SetObjectToControlValue(this, data);
            var dinnerSaleId = "";
            if (data != null) dinnerSaleId = data.Id;
            dgvDinnerSaleItemUse.DataSource = LgcBill.GetBillItemView(" where T_BillItem.DinnerSaleId ='" + dinnerSaleId + "'");
        }

        private void tlSave_Click(object sender, EventArgs e)
        {
            var dinnerSalePayWindow = new DinnerSalePayWindow();
            dinnerSalePayWindow.SetMember(Member.Id);
            dinnerSalePayWindow.ShowDialog();
            if (dinnerSalePayWindow.DialogResult == DialogResult.OK)
            {
                DinnerSales = DinnerSaleBll.GetDinnerSalesView(" where t1.MemberId ='" + Member.Id + "'");
                dgvDinnerSale.DataSource = DinnerSales;
            }
        }

        private void tlDinnerBack_Click(object sender, EventArgs e)
        {
            if (dgvDinnerSale.CurrentRow == null) return;
            var id = dgvDinnerSale.CurrentRow.Cells["DinnerSaleId"].Value.ToString();
            var state = dgvDinnerSale.CurrentRow.Cells["State"].Value.ToString();
            if (state == "已退办")
            {
                FormSysMessage.ShowMessage("该套餐已经退办，不能再退");
                return;
            }
            var dinnerSaleBack = new DinnerSaleBackWindow();
            dinnerSaleBack.SetDinnerSale(id);
            dinnerSaleBack.ShowDialog();
            if (dinnerSaleBack.DialogResult == DialogResult.OK)
            {
                DinnerSales = DinnerSaleBll.GetDinnerSalesView(" where t1.MemberId ='" + Member.Id + "'");
                dgvDinnerSale.DataSource = DinnerSales;
            }
        }
    }
}
